package br.net.woodstock.tickettoride.api.spring.security.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;

public class TicketUserDetailsService implements UserDetailsService {

	@Autowired(required = true)
	private UserService	userService;

	@Override
	public UserDetails loadUserByUsername(final String username) {
		User user = this.userService.getByLogin(username);
		if (user == null) {
			throw new UsernameNotFoundException("User not found: " + username);
		}

		return new TicketUserDetails(user);
	}

}
